import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import '../config/httpHeaders.dart';

class MyPage extends StatefulWidget {
  @override
  _MyPageState createState() {
    // TODO: implement createState
    return _MyPageState();
  }
}

class _MyPageState extends State<MyPage>{
  int _counter = 0;
  TextEditingController tvc = new TextEditingController();
  String showText = '欢迎你来到美好人间';

  get child => null;

  @override
  Widget build(BuildContext context) {
    
    return Scaffold(
      appBar: AppBar(
        title:Text('我的'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextField(
              controller: tvc,
              cursorColor: Colors.green,// 光标颜色
              decoration: InputDecoration(
                contentPadding: EdgeInsets.all(10),
                labelText: '',
                helperText: '请输入你喜欢的类型',
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.all(Radius.circular(5.0)),
                )
              ),
              autofocus: true,
              maxLines: 1,
              onChanged: (str){
                setState(() {
                  print('改变'+str);
                });
              },
            ),
            Text(
              showText,
              overflow: TextOverflow.ellipsis,
              maxLines: 2,
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
            RaisedButton(
              child: Text('发起网络请求'),
              onPressed:(){
                    setState(() {
                      print('发起网络请求');
                      // getHttpWithHeader().then((val){
                      //     showText = val['data'].toString();
                      // });
                    });
              }),
          ], 
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.cloud_upload),
      ), //
      
    );    
  }

  void _incrementCounter () {
      // setState(() {
      //    _counter++;
      // });
      if(tvc.text.toString() == ''){
        showDialog(
          context: context,
          builder: (context) => AlertDialog(title: Text('输入框不能为空')),
        );
      }else {
        getHttp(tvc.text.toString()).then((val){
            setState(() {
              print('输出'+ val.toString());
              showText = val['data']['name'].toString();
            });
          }
        );
      }
    }
}

Future getHttp(String text) async {
  try {
    Response response;
    var data= {'name':text};
    response = await Dio().get(
        "https://www.easy-mock.com/mock/5c60131a4bed3a6342711498/baixing/dabaojian",
        queryParameters: data,
    );
    return response.data;
  } catch (e) {
    return print(e);
  }
}

Future getHttpWithHeader() async{
  try {
    Response response;
    Dio dio = new Dio();
    dio.options.headers = httpHeaders;
    response = await Dio().get(
        "https://time.geekbang.org/serv/v1/column/newAll",
    );
    return response.data;
  } catch (e) {
    return print(e);
  }
}
